﻿/******************************************
 * ** di Carbone Carlo                    *
 * ** carbone05@gmail.com                 *
 * ***************************************/

using System;

namespace Badge_Manager
{
    public class DatiDB
    {
        #region "Proprietà"
            protected IniManager ini;
            public string Server { get; set; }
            public int Porta { get; set; }
            public string NomeDB { get; set; }
            public string Utente { get; set; }
            public string Password { get; set; }
            public string PercorsoINI { get; set; }
        #endregion

        #region "Costruttore"

            public DatiDB(string pathconfig, string server, string nomedb
                , string utente, string password, int porta = 0)
            {
                this.PercorsoINI = pathconfig;
                ini = new IniManager(this.PercorsoINI);

                this.Server = server;
                this.Porta = porta;
                this.NomeDB = nomedb;
                this.Utente = utente;
                this.Password = password;
            }

            public DatiDB(string pathconfig)
            {
                this.PercorsoINI = pathconfig;
                ini = new IniManager(this.PercorsoINI);
            }

            public DatiDB() { }
        #endregion

        #region "Metodi"
            public override string ToString()
            {
                string cnn;
                cnn = "Data Source=" + this.Server;
                if (Convert.ToInt16(this.Porta) != 0)
                    cnn += "," + Convert.ToInt16(this.Porta);
                cnn += ";Initial Catalog=" + this.NomeDB;
                cnn += ";User=" + this.Utente;
                cnn += ";Password=" + this.Password;

                return cnn;
            }

            public void Salva()
            {
                ini.IniWriteValue("database", "server", this.Server);
                ini.IniWriteValue("database", "nomedb", this.NomeDB);
                ini.IniWriteValue("database", "porta", this.Porta.ToString());
                ini.IniWriteValue("database", "utente", this.Utente);
                ini.IniWriteValue("database", "password", this.Password);
            }

            public DatiDB Leggi()
            {
                this.Server = ini.IniReadValue("database", "server");
                this.Porta = Convert.ToInt16(ini.IniReadValue("database", "porta"));
                this.NomeDB = ini.IniReadValue("database", "nomedb");
                this.Utente = ini.IniReadValue("database", "utente");
                this.Password = ini.IniReadValue("database", "password");

                DatiDB tmp;
                tmp = new DatiDB("", this.Server
                , this.NomeDB
                , this.Utente
                , this.Password
                , Convert.ToInt16(this.Porta));
                return tmp;
            }
        #endregion
    }
}
